<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/17
 * Time: 上午11:28
 */
class Lyw_original_article_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function add($user_id, $title, $article_text, $article_html, $url = '')
    {
        $this->db->trans_start();
        $data = array('original_user_id' => $user_id, 'title' => $title, 'article_text' => $article_text, 'article_html' => $article_html, 'url' => $url, 'creat_time' => time());
        $str = $this->db->insert_string('lyw_original_article', $data);
        $this->db->query($str);
        $insert_id = $this->db->insert_id();
        $this->load->model('Lyw_article_manager_model');
        $manager_insert_id = $this->Lyw_article_manager_model->originalStatus($insert_id, $title);
        $this->db->trans_complete();
        if ($manager_insert_id > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function getOneListInfoById($id, $arr = array())
    {
        if (empty($arr)) {
            $sql = 'select * from lyw_original_article where id = ?';
        } else {
            $str = arrToStr($arr);
            $sql = "select {$str} from lyw_original_article where id = ?";
        }
        $result = $this->db->query($sql, array($id))->result_array();
        if (!empty($result)) {
            return $result[0];
        } else {
            return array();
        }
    }

    public function getIdListByUserid($user_id)
    {
        $sql = 'select id from lyw_original_article where original_user_id = ?';
        $result = $this->db->query($sql, array($user_id))->result_array();
        return $result;
    }

    /**
     * 前面有个add方法,是在用户领取任务的时候产生的,这个方法是用户开始翻译的时候添加,其实就是update
     */
    public function saveAdd($id, $manager_id, $user_id, $title, $article_text, $article_html, $url)
    {
        $this->db->trans_start();
        $data = array('title' => $title, 'article_text' => $article_text, 'article_html' => $article_html, 'update_time' => time(), 'url' => $url);
        $where = "id = {$id} and original_user_id = {$user_id}";//必须带user_id ,有验证权限功能
        $str = $this->db->update_string('lyw_original_article', $data, $where);
        $this->db->query($str);
        $this->load->model('Lyw_article_manager_model');
        $this->Lyw_article_manager_model->updateTitle($manager_id, $title);
        $this->db->trans_complete();
        return true;
    }
}